package com.wlzg.collectiontool.view

import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.PositionPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.adapter.XiangmuAdapter
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.PsXiangmu
import kotlinx.android.synthetic.main.dia_prolist.view.*
import kotlinx.android.synthetic.main.dia_prolist.view.diss
import kotlinx.android.synthetic.main.new_xiaoqu.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ProlistDia(var mContext: Context, var mFragment: ManageFragment) :
    PositionPopupView(mContext),
    View.OnClickListener {
    private var dataList: ArrayList<PsXiangmu?>? = ArrayList()
    private var mAdapter: XiangmuAdapter = XiangmuAdapter()
    private var xmPop: BasePopupView? = null
    private var xmdia: NewXmDia? = null
    override fun getImplLayoutId(): Int {
        return R.layout.dia_prolist
    }

    override fun onCreate() {
        super.onCreate()
        diss.setOnClickListener(this)
        sousuo.setOnClickListener(this)
        initData()
        initListener()

    }
    fun onUpdate(){
        mAdapter.notifyDataSetChanged()
    }


    fun initPopDia(t: PsXiangmu?) {
        xmdia = NewXmDia(context, mFragment, t,this)
        xmPop = XPopup.Builder(context).dismissOnTouchOutside(false)
            .borderRadius(15.0f)
            .asCustom(xmdia)
    }

    private fun initListener() {
        mAdapter.addChildClickViewIds(
            R.id.delete,
            R.id.bianji
        )
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.delete -> {
                    mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
                        withContext(Dispatchers.IO) {
                            dataList?.get(position)?.let {


                                   if( AppDatabase.getInstance().userDao().getGcListBypid(
                                        it.xiangmuId!!
                                    )!=null){
                                        ToastUtils.showShort("该项目存在关联工程,小区或者道路")
                                       return@withContext
                                   }


                                AppDatabase.getInstance().userDao().deleteXm(
                                    it
                                )
                                dataList?.removeAt(position)
                            }
                        }
                        mAdapter.notifyDataSetChanged()
                        mFragment.ondelete(1,null)
                    }

                }
                R.id.bianji -> {
                    initPopDia(dataList?.get(position))
                    if (!(xmPop?.isShow!!)) {
                        xmPop?.show()
                    }


                }
            }


        }
    }

    private fun initData() {
        recycler.adapter = mAdapter
        recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mAdapter.setNewData(dataList)
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                val list = AppDatabase.getInstance().userDao().getXmList() as ArrayList<PsXiangmu?>?
                list?.let { dataList?.addAll(it) }

            }
            mAdapter.notifyDataSetChanged()

        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.diss -> {
                dismiss()
            }
            R.id.sousuo -> {
               var name=ssxmmc.text.toString()
                mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        val list = AppDatabase.getInstance().userDao().getXmListbyname(name) as ArrayList<PsXiangmu?>?
                        list?.let {
                            dataList?.clear()
                            dataList?.addAll(it) }

                    }
                    mAdapter.notifyDataSetChanged()

                }
            }
        }
    }
}